package edu.ccut.saturn.studio.dialog.decisiondialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDecisionNode;

public class SaturnDecisionDialog extends TitleAreaDialog {
	
private Text idText;
private Text nameText, expressionText, descriptionText;
	
	private Label nameLabel, expressionLabel, descriptionLabel;
	
	private SaturnDecisionNode saturnDecisionNode;

	public SaturnDecisionDialog(Shell parentShell, SaturnDecisionNode saturnDecisionNode){
		super(parentShell);
		this.saturnDecisionNode = saturnDecisionNode;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnDecisionNode.getName()); 
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		
		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING); 
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final Label idLabel = new Label(container, SWT.NONE);
		idLabel.setText(Messages.getString("SaturnDecisionDialog.ID")); //$NON-NLS-1$

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idText.horizontalIndent = 10;
		gd_idText.verticalIndent = 10;
		idText.setLayoutData(gd_idText);

		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 10;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnDecisionDialog.name"));   //$NON-NLS-1$
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_nameText.horizontalIndent = 10;
		gd_nameText.verticalIndent = 10;
		nameText.setLayoutData(gd_nameText);

		expressionLabel = new Label(container, SWT.NONE);
		final GridData gd_expressionLabel = new GridData();
		gd_expressionLabel.verticalIndent = 10;
		expressionLabel.setLayoutData(gd_expressionLabel);
		expressionLabel.setText(Messages.getString("SaturnDecisionDialog.expression"));  //$NON-NLS-1$
		
		expressionText = new Text(container, SWT.BORDER);
		expressionText.setTextLimit(150);
		final GridData gd_expressionText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_expressionText.horizontalIndent = 10;
		gd_expressionText.verticalIndent = 10;
		expressionText.setLayoutData(gd_expressionText);

		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.TOP, false, false);
		gd_descriptionLabel.verticalIndent = 14;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("SaturnDecisionDialog.description"));   //$NON-NLS-1$
		
		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_descriptionText = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_descriptionText.horizontalIndent = 10;
		gd_descriptionText.verticalIndent = 10;
		descriptionText.setLayoutData(gd_descriptionText);
		
		idText.setText(saturnDecisionNode.getId());
//		idText.setSelection(saturnDecisionNode.getId().length());
		nameText.setText(saturnDecisionNode.getName());
		nameText.setSelection(saturnDecisionNode.getName().length());
		expressionText.setText(saturnDecisionNode.getExpression());
		descriptionText.setText(saturnDecisionNode.getDescription());
		
		/**
		 * 名称不可为空判断
		 * 
		 * */
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if("".equals(nameText.getText().trim())){
					setErrorMessage(Messages.getString("SaturnDecisionDialog.NameLimit"));  //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}else{
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		
		return area;
	}
	
	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnDecisionNode.setDescription(descriptionText.getText().trim());
			saturnDecisionNode.setExpression(expressionText.getText().trim());
			saturnDecisionNode.setName(nameText.getText().trim());
		}
		super.buttonPressed(buttonId);
	}
	
	protected Point getInitialSize() {
			
		return new Point(530, 300);
	}
	
	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}
}
